package com.w1.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.w1.security.Mapper.UserMapper;
import com.w1.security.domain.User;
import com.w1.security.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

/**
 * @author mingyifan
 * @date 2022-03-07 17:26
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public int updateUserWinGames(String  id) {
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("id", id);
        User user = userMapper.selectOne(wrapper);
        HashMap<String, Object> map = new HashMap<>();
        float a = user.getAllGames() + 1;
        float w = user.getWinGames()+1;
        float  num=(w/a)*100;
//        float a = user.getAllGames() + 1;
//        Long w = user.getWinGames()+1;
//        double sl=(w/a)*100/100;
//        float  totalPrice=w/a;
//        float  num=( float )(Math.round(totalPrice* 100 ))/ 100 ; //如果要求精确4位就*10000然后/10000
        map.put("id",id);
        map.put("all",a);
        map.put("win",w);
        map.put("percentage",num);
        System.out.println(num);
        //指定id用户+1胜场
        return userMapper.updateUserWinGames(map);
    }

    @Override
    public Page<User> getUserPage(QueryWrapper<User> wrapper) {
        return userMapper.selectPage(new Page<>(0,10),wrapper);
    }
}
